define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/index' + location.search,
                    add_url: 'goods/add',
                    edit_url: 'goods/edit',
                    del_url: 'goods/del',
                    multi_url: 'goods/multi',
                    import_url: 'goods/import',
                    table: 'goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'cate_id', title: __('Cate_id')},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'en_name', title: __('En_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'sales_num', title: __('Sales_num')},
                        {field: 'type', title: __('区域'), searchList: {"1":__('金钻'),"2":__('银钻')}, formatter: Table.api.formatter.normal},

                        {field: 'is_recommend', title: __('Is_recommend'), searchList: {"1":__('Is_recommend 1'),"2":__('Is_recommend 2')}, formatter: Table.api.formatter.normal},
                        {field: 'is_open', title: __('是否上架'), searchList: {"1":__('Is_recommend 1'),"2":__('Is_recommend 2')}, formatter: Table.api.formatter.normal},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'bazaar_money', title: __('Bazaar_money'), operate:false},
                        {field: 'stock', title: __('Stock'), operate:false},
                        {field: 'unit', title: __('Unit'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'en_unit', title: __('En_unit'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(".freight_money").hide();
            $(document).on("change", "#c-is_by", function (e){
                var is_by = $("#c-is_by").val();
                if (is_by == 1){
                    $(".freight_money").hide();
                }else if (is_by == 2){
                    $(".freight_money").show();
                }
            });
            Controller.api.bindevent();
        },
        edit: function () {
            var is_by = $("#c-is_by").val();
            if (is_by == 1){
                $(".freight_money").hide();
            }else if (is_by == 2){
                $(".freight_money").show();
            }
            $(document).on("change", "#c-is_by", function (e){
                var is_by = $("#c-is_by").val();
                if (is_by == 1){
                    $(".freight_money").hide();
                }else if (is_by == 2){
                    $(".freight_money").show();
                }
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
